<?php

if (!isset($website) ) { header('HTTP/1.1 404 Not Found'); die; }

if ( !is_logged() ) {

  if ( file_exists( $smf_forum .'SSI.php' ) ) {
	 include($smf_forum .'SSI.php');
	 
	 global $context, $txt, $scripturl;
	 
	 if ($context["user"]["id"]>=1) {
	    $SMF_id = $context["user"]["id"]; 
		$SMF_session_var = $context['session_var'];
		$SMF_sid = &$context['session_id'];
	    $SMF_username = $context["user"]["name"]; 
		$SMF_email = $context["user"]["email"]; 
		$SMF_website = $smf_forum_url."index.php?action=profile;u=".$SMF_id; 
        $userID=loadMemberData($SMF_username, true, 'profile');
        loadMemberContext($userID[0]);
        $SMF_avatar = $memberContext[$userID[0]]['avatar']['href'];
		
		require_once('inc/class.database.php');
		require_once('inc/db_connect.php');

         $check = $db->query("SELECT * FROM ".OSDB_USERS." 
		 WHERE user_email = '".$SMF_email."' AND smf_id = '".$SMF_id."'");
		 
		if ( $db->num_rows($check)<=0 ) {
	    //CREATE NEW USER (from phpbb database)
	    $checkUn = $db->query("SELECT * FROM ".OSDB_USERS." WHERE LOWER(user_name) = LOWER('".$SMF_username."') ");
	    if ( $db->num_rows($checkUn)>=1 ) {
	    $SMF_username = $SMF_username."_".$SMF_id;
		$pass = generate_hash(5);
		$hash = generate_hash(12);
		$password_db = generate_password($pass, $hash); 
		
		$insert = $db->query("INSERT INTO ".OSDB_USERS."(user_name, user_email, user_password, password_hash, user_joined, user_level, user_last_login, user_ip, user_avatar, smf_id, user_website )
	   VALUES('".$SMF_username."', '".$SMF_email."', '".$password_db."', '".$hash."', '".(int) time()."', '0', '".(int) time()."', '".safeEscape($_SERVER["REMOTE_ADDR"])."', '".$SMF_avatar."', '".$SMF_id."', '".$SMF_website."' )");
	   $id = $db->get_insert_id();
	   $_SESSION["user_id"] = $id ;
	   $_SESSION["username"] =$SMF_username;
	   $_SESSION["email"]    = $SMF_email;
	   $_SESSION["level"]    = 0;
	   $_SESSION["can_comment"]    = 1;
	   $_SESSION["logged"]    = time();
	   $_SESSION["smf"]    = $SMF_id;
	   //$_SESSION["logout"]    = $smf_forum_url."?action=logout;".$SMF_session_var."=".$SMF_sid;
	   $logout = $scripturl . '?action=logout;' . $SMF_session_var . '=' . $SMF_sid ;
	   //Maybe SMF bug. Session verification not working...set forum link instead logout link.
	   $logout =$smf_forum_url;
	   $_SESSION["logout"]    = $logout;
	    }
		
	   }
	   else {
	  //UPDATE USER DATA
	  $row = $db->fetch_array($check,'assoc');
	  
	  $update = $db->query("UPDATE ".OSDB_USERS." SET 
	  user_last_login = '".(int) time()."',
	  user_avatar = '".$SMF_avatar."'
	  WHERE user_id = '".$row["user_id"]."' LIMIT 1");
	   $_SESSION["user_id"] = $row["user_id"] ;
	   $_SESSION["username"] = $row["user_name"];
	   $_SESSION["email"]    = $SMF_email;
	   $_SESSION["level"]    = $row["user_level"];
	   $_SESSION["can_comment"]    = 1;
	   $_SESSION["logged"]    = time();
	   $_SESSION["smf"]    = $SMF_id;
	   //$_SESSION["logout"]    = $smf_forum_url."?action=logout;".$SMF_session_var."=".$SMF_sid;
	   $logout = $scripturl . '?action=logout;' . $SMF_session_var . '=' . $SMF_sid ;
	   //Maybe SMF bug. Session verification not working on logout...set forum link instead logout link.
	   $logout =$smf_forum_url;
	   $_SESSION["logout"]    = $logout;
	  }

	   
	}
  }
}
?>